{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 一、函数\n",
    "## 1.1 参数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "## 1.位置参数，严格按照位置顺序，用实参对形参进行赋值（关联）\n",
    "def add(a,b):\n",
    "    return a+b\n",
    "\n",
    "add(1,2)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "## 2.关键字参数，打破位置顺序，用关键字对形参进行赋值（形参=实参）\n",
    "def add(a,b):\n",
    "    return a+b\n",
    "\n",
    "add(a=1,b=2)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "dw\n",
      "dgl\n"
     ]
    }
   ],
   "source": [
    "## 3.默认参数\n",
    "#注：默认参数应该设置为不可变类型\n",
    "def add(a=0,b=0):\n",
    "    return a+b\n",
    "\n",
    "add()\n",
    "print(add(1))\n",
    "print(add(1,2))\n",
    "\n",
    "##让参数变成可选的\n",
    "def name(first_name , last_name , middle_name=None ):\n",
    "    if middle_name:\n",
    "        return first_name + middle_name + last_name\n",
    "    else:\n",
    "        return first_name + last_name\n",
    "print(name(\"d\",\"w\"))\n",
    "print(name(\"d\",\"l\",\"g\"))\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T09:37:01.503867300Z",
     "start_time": "2024-11-20T09:37:01.490143400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3\n",
      "(4, 5, 6)\n"
     ]
    },
    {
     "data": {
      "text/plain": "15"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 4.不定长参数,多余的参数打包成元组，放到args\n",
    "def add(a,b,c,*args):\n",
    "    print(a,b,c)\n",
    "    print(args)\n",
    "    return sum(args)\n",
    "\n",
    "add(1,2,3,4,5,6)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T09:39:06.826748700Z",
     "start_time": "2024-11-20T09:39:06.811397500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "## 5.可变长参数 **kargs"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3\n",
      "{'a': 4, 'b': 5, 'c': 6}\n"
     ]
    }
   ],
   "source": [
    "def foo (x,y,z,**kwargs):\n",
    "    print(x,y,z)\n",
    "    print(kwargs)\n",
    "foo(1,2,3,a=4,b=5,c=6)#多余的参数，以字典形式打包传递给kwargs\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T09:42:43.141061700Z",
     "start_time": "2024-11-20T09:42:43.112568700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 6.匿名函数lambda\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(67, 87), (76, 94), (88, 91), (90, 92)]\n",
      "[(67, 87), (88, 91), (90, 92), (76, 94)]\n",
      "(67, 87)\n",
      "(76, 94)\n"
     ]
    }
   ],
   "source": [
    "#常用用法\n",
    "#排序sort(),sorted()\n",
    "ls= [(76,94),(67,87),(88,91),(90,92)]\n",
    "ls.sort()\n",
    "print(ls)\n",
    "\n",
    "ls.sort(key = lambda x:x[1])#根据x[1]进行排序（即根据第二个数排序）\n",
    "print(ls)\n",
    "\n",
    "#min\n",
    "n=min(ls,key = lambda x:x[1])\n",
    "print(n)\n",
    "\n",
    "#max\n",
    "m=max(ls,key = lambda x:x[1])\n",
    "print(m)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-20T10:53:43.892495Z",
     "start_time": "2024-11-20T10:53:43.866798900Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
